$(function() {

    var $paySubmit = $('.pay-submit');
    var $actualpay = $('.e_actualpay');
    var $points = $('.e_points');
    var backUrl = $('#backUrl').val();
    var startTime;
    var timer = null;

    $paySubmit.click(function() {

        var $payMoney = $('.pay-money .cur');
        var $payPlatForm = $('.pay-bank-info .cur');
        var payMoney = 0;
        var payPlatForm = 0;
        var _this =this;
        
        if ($payMoney.length) {
            var $isPayMoney = $payMoney.find('a');
            var $isCustomPayMoney = $payMoney.find('input');
            if ($isPayMoney.length) {
                payMoney = $isPayMoney.attr('data-money') * 1;
            }

            if ($isCustomPayMoney.length) {
                payMoney = $isCustomPayMoney.val();
            }

        } else {
            alert('请选择要充值的金额');
            return;
        }

        if ($payPlatForm.length) {
            var $isPayPlatFormType = $payPlatForm.find('a');
            if ($isPayPlatFormType.length) {
                payPlatForm = $isPayPlatFormType.attr('data-bank');
            }
        } else {
            alert('请选择进行充值的平台！');
            return;
        }

        if (!payMoney) {
            alert('充值金额不能为空！');
            return;
        } else {
            if (!/^\d+$/.test(payMoney)) {
                alert('充值金额必须为大于0 的整数，请重新输入！');
                return;
            }
        }

        if (!payPlatForm) {
            alert('请选择进行充值的平台！');
            return;
        }
        
        _this.disabled=true;
        
    	$('.qrcode-box iframe').attr('src','');
    	$('.qrcode-box').hide();
    	$('.timeout').hide();
     	$('.success-ok').hide();
    	
        $('.loading').show();
        
        $.get('ylw_qr_pay.htm', {
            'money': payMoney,
            'paytype': payPlatForm
        }, function(data) {
            if (data.errorCode === 0) {
            // data.bizObj.orderId
            // ylw_qr_pay_check_status.htm 
            // orderId

                $('iframe').attr('src', data.bizObj.payQrUrl);
                $('.loading').hide()
                $('.qrcode-box').show();
                
        		clearTimeout(timer);
                startTime = new Date() * 1;
                checkState(data.bizObj.orderId);
                
                _this.disabled=false;
                
            } else {
                alert('系统内部错误，错误编码：' + data.errorCode)
            }
        }, 'json').error(function(){
        	_this.disabled=false;
        });

    });


    $('.pay-money li').click(function() {

        var $isPayMoney = $(this).find('a');
        var $isCustomPayMoney = $(this).find('input');
        var value = 0;
        if ($isPayMoney.length) {
            value = $isPayMoney.attr('data-money') * 1;
            $actualpay.html(value);
            $points.html(value * 100);
        }

        if ($isCustomPayMoney.length) {
            $isCustomPayMoney.off('keyup,focus');
            $isCustomPayMoney.on('keyup focus',function(){
                value = $isCustomPayMoney.val();
                if (/^\d+$/.test(value)) {
                    $actualpay.html(value);
                    $points.html(value * 100);
                }
            });
        }
        
        clearTimeout(timer);

        $(this).addClass('cur').siblings().removeClass('cur');

        if ($('.qrcode-box').is(':visible')) {
        	$('.qrcode-box iframe').attr('src','');
        	$('.qrcode-box').hide();
        }
       
    });

    $('.pay-bank-info li').click(function() {
        clearTimeout(timer);
        $(this).addClass('cur').siblings().removeClass('cur');
        if ($('.qrcode-box').is(':visible')) {
        	$('.qrcode-box iframe').attr('src','');
        	$('.qrcode-box').hide();
        }
    });

    if (backUrl) {
        $('.backurl-btn').attr('href', backUrl).show();
    }
    
    function checkState(orderId){
    	 $.post('ylw_qr_pay_check_status.htm', {
            'orderId': orderId
        }, function(data) {
            if (data.errorCode === 0) {
            //  成功地址 data.bizObj
            	$('.qrcode-box').hide();
        		$('.qrcode-box iframe').attr('src','');
        		$('.timeout').hide();
           		$('.success-ok').show();
            } else {
            	if( new Date() * 1 - startTime >= 180000){
            		$('.qrcode-box').hide();
            		$('.qrcode-box iframe').attr('src','');
            		$('.success-ok').hide();
            		$('.timeout').show();
            		return ;
            	}
                timer = setTimeout(function(){
                	checkState(orderId);
                },2000);
            }
        }, 'json');
    }

});
